//
//  ChatListCell.m
//  HiCar
//
//  Created by huachun on 2018/12/19.
//  Copyright © 2018 丁一. All rights reserved.
//

#import "ChatListCell.h"
#import "ChatModel.h"
#import "OSSTestMacros.h"
#import "ChatSql.h"
#import "HiJonesClass.h"

@interface ChatListCell ()

@property (nonatomic, strong) UIImageView *Userlogo;
@property (nonatomic, strong) UIImageView *Userlogo_img;
@property (nonatomic, strong) UILabel     *Username;
@property (nonatomic, strong) UILabel     *Usercontent;
@property (nonatomic, strong) UILabel     *date;

@property (nonatomic, strong) UILabel     *TXlabel;

@end

@implementation ChatListCell

+ (instancetype)cellForTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"ChatListCell";
    ChatListCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell){
        cell=[[ChatListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor=[UIColor whiteColor];
        tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

        [cell.contentView addSubview:cell.Userlogo];
        [cell.contentView addSubview:cell.Username];
        [cell.contentView addSubview:cell.Usercontent];
        [cell.contentView addSubview:cell.date];
        [cell.contentView addSubview:cell.TXlabel];
    }
    
    return cell;
}

- (void)setModel:(ChatModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
    _Usercontent.hidden = YES;
    _date.hidden = YES;
    _Userlogo_img.hidden=YES;
    if ([_Model.Chat_type isEqualToString:@"5"]||[_Model.Chat_type isEqualToString:@"6"]||[_Model.Chat_type isEqualToString:@"7"]||[_Model.Chat_type isEqualToString:@"8"]||[_Model.Chat_type isEqualToString:@"9"]||[_Model.Chat_type isEqualToString:@"18"]||[_Model.Chat_type isEqualToString:@"10"]||[_Model.Chat_type isEqualToString:@"20"]) {
        _TXlabel.frame = CGRectMake(RECRWITCH-40, 42.5, 20, 20);
        self.accessoryType=UITableViewCellAccessoryNone;
        _Usercontent.hidden = NO;
        _date.hidden = NO;
        _Userlogo.frame = CGRectMake(20, 15, 50, 50);
        NSString *imgurl = _Model.User_logo;
        [_Userlogo sd_setImageWithURL:[NSURL URLWithString:imgurl] placeholderImage:[UIImage imageNamed:@"iconlogin.png"]];
        _Username.text = _Model.User_name;
        _Username.frame = CGRectMake(80, 15, RECRWITCH-110, 25);
        _Usercontent.text = _Model.Chat_info;
        if ([_Model.Chat_type isEqualToString:@"8"]||[_Model.Chat_type isEqualToString:@"18"]) {
            _Usercontent.text = @"[照片]";
        }else if ([_Model.Chat_type isEqualToString:@"10"]||[_Model.Chat_type isEqualToString:@"20"]){
            _Usercontent.text = @"[活动动态]";
        }
        _date.text = [HiJonesClass timeZhuan:_Model.Chat_date];
        if ([_Model.IsRead isEqualToString:@"1"]) {
            _TXlabel.hidden=YES;
        }else{
            NSMutableArray *linarray = [[[[ChatSql alloc] init] selectdatanewstixing:_Model.User_id] mutableCopy];
            
            if (linarray.count>0) {
                _TXlabel.hidden=NO;
                _TXlabel.text=[NSString stringWithFormat:@"%lu",(unsigned long)linarray.count];
                if (linarray.count>100) {
                    _TXlabel.frame = CGRectMake(RECRWITCH-50, 42.5, 30, 20);
                }else{
                    _TXlabel.frame = CGRectMake(RECRWITCH-40, 42.5, 20, 20);
                }
            }else{
                _TXlabel.hidden=YES;
            }
        }
    } else {
        _TXlabel.frame = CGRectMake(RECRWITCH-50, 25, 20, 20);
        self.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        _Userlogo.frame = CGRectMake(20, 10, 50, 50);
        _Userlogo_img.hidden = NO;
        _Username.frame = CGRectMake(80, 10, RECRWITCH-110, 50);
        if ([_Model.Chat_type isEqualToString:@"1"]) {
            _Username.text = @"喜欢";
            _Userlogo_img.image = [UIImage imageNamed:@"chatxihuan"];
            _Userlogo.backgroundColor = [JonesClass stringTOColor:@"#d4237a"];
        } else if ([_Model.Chat_type isEqualToString:@"2"]){
            _Username.text = @"活动";
            _Userlogo_img.image = [UIImage imageNamed:@"chathuodong"];
            _Userlogo.backgroundColor = [JonesClass stringTOColor:@"#1296db"];
        } else if ([_Model.Chat_type isEqualToString:@"3"]){
            _Username.text = @"评论";
            _Userlogo_img.image = [UIImage imageNamed:@"chatpinglun"];
            _Userlogo.backgroundColor = [JonesClass stringTOColor:@"#1faf08"];
        } else if ([_Model.Chat_type isEqualToString:@"4"]){
            _Username.text = @"赞";
            _Userlogo_img.image = [UIImage imageNamed:@"chatdianzan"];
            _Userlogo.backgroundColor = [JonesClass stringTOColor:@"#d8ce0c"];
        }
        
        if (_Model.Count==0||_Model.Count<0) {
            _TXlabel.hidden=YES;
        }else{
            _TXlabel.hidden=NO;
            _TXlabel.text=[NSString stringWithFormat:@"%lu",(unsigned long)_Model.Count];
            if (_Model.Count>100) {
                _TXlabel.frame = CGRectMake(RECRWITCH-65, 25, 30, 20);
            }else{
                _TXlabel.frame = CGRectMake(RECRWITCH-55, 25, 20, 20);
            }
        }
        
//        else if ([_Model.Chat_type isEqualToString:@"5"]){
//            _Username.text = @"系统";
//            _Userlogo_img.image = [UIImage imageNamed:@"chatlaba"];
//            _Userlogo.backgroundColor = [JonesClass stringTOColor:@"#0c64d8"];
//        }
    }
}

-(UIImageView *)Userlogo{
    if (!_Userlogo) {
        _Userlogo                     = [[UIImageView alloc] initWithFrame:CGRectMake(20, 15, 50, 50)];
        _Userlogo.layer.cornerRadius  = 25;
        _Userlogo.layer.masksToBounds = YES;
        _Userlogo.contentMode         = UIViewContentModeScaleAspectFill;
        _Userlogo_img = [[UIImageView alloc] initWithFrame:CGRectMake(11, 11, 28, 28)];
        _Userlogo_img.contentMode     = UIViewContentModeScaleAspectFill;
        [_Userlogo addSubview:_Userlogo_img];
    }
    return _Userlogo;
}

-(UILabel *)Username{
    if (!_Username) {
        _Username = [[UILabel alloc] initWithFrame:CGRectMake(80, 15, RECRWITCH-110, 25)];
        [_Username setFont:[UIFont systemFontOfSize:17]];
        [_Username setTextColor:[JonesClass stringTOColor:@"#111111"]];
    }
    return _Username;
}

-(UILabel *)Usercontent{
    if (!_Usercontent) {
        _Usercontent = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, RECRWITCH-120, 25)];
        [_Usercontent setFont:[UIFont systemFontOfSize:13]];
        [_Usercontent setTextColor:[JonesClass stringTOColor:@"#888888"]];
    }
    return _Usercontent;
}

-(UILabel *)date{
    if (!_date) {
        _date = [[UILabel alloc] initWithFrame:CGRectMake(RECRWITCH-110, 15, 90, 25)];
        [_date setFont:[UIFont systemFontOfSize:12]];
        _date.textAlignment=NSTextAlignmentRight;
        [_date setTextColor:[JonesClass stringTOColor:@"#999999"]];
    }
    return _date;
}

-(UILabel *)TXlabel{
    if (!_TXlabel) {
        _TXlabel = [[UILabel alloc] initWithFrame:CGRectMake(RECRWITCH-40, 42.5, 20, 20)];
        [_TXlabel setFont:[UIFont boldSystemFontOfSize:11]];
        _TXlabel.text=@"10";
        _TXlabel.textAlignment=NSTextAlignmentCenter;
        [_TXlabel setTextColor:[UIColor whiteColor]];
        _TXlabel.backgroundColor=[UIColor redColor];
        _TXlabel.layer.cornerRadius=10;
        _TXlabel.layer.masksToBounds=YES;
    }
    return _TXlabel;
}

@end
